//
//  ListenViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "ListenViewController.h"

@interface ListenViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,SkipVC>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *topArr;
@property (nonatomic, retain) UICollectionView *bottomCollectionView;
@property (nonatomic, retain) NSMutableArray *tableCellModelArr;
@property (nonatomic, retain) NSMutableArray *tableCellSecModelArr;
@property (nonatomic, retain) NSMutableArray *tableCellThirdModelArr;
//用来标记底层collectionView的数量
@property (nonatomic, assign) NSInteger num;
//导航条的下面的线
@property (nonatomic, retain) UIView *lineView;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation ListenViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.pageCount = 0;
        self.tableCellThirdModelArr = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

    
    [self createTopCollection];
    
    [self createBottomCollection];
    
    [self dataHandle];
    
    
}

///数据处理
-(void)dataHandle{
    [APPTools GETWithURL:@"http://mobile.ximalaya.com/mobile/api/1/feed/unlogin/dynamic?device=iPhone" par:nil success:^(id responseObject) {
        self.tableCellModelArr = [ListenModel modelHandleWithArray:responseObject[@"data"]];
        self.num = 3;
        [_bottomCollectionView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
    [APPTools GETWithURL:@"http://mobile.ximalaya.com/mobile/album/recommend/list/unlogin?device=iPhone" par:nil success:^(id responseObject) {
        self.tableCellSecModelArr = [ListenModel modelHandleWithArray:responseObject[@"frDatas"]];
        [_bottomCollectionView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}









///最上面的collectionView
-(void)createTopCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 3, 30);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 37) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    
    self.navigationItem.titleView = _collectionView;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:@"TopCollectionViewCell"];
    
    //topCollection的数组
    self.topArr = @[@"关注", @"收藏", @"历史"];
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 35, self.view.frame.size.width / 3, 2)];
    _lineView.backgroundColor = [UIColor orangeColor];
    [_collectionView addSubview:_lineView];
}


///最底层的collectionView
-(void)createBottomCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 40);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height -40) collectionViewLayout:layout];
    _bottomCollectionView.delegate = self;
    _bottomCollectionView.dataSource = self;
    
    [self.view addSubview:_bottomCollectionView];
    _bottomCollectionView.pagingEnabled = YES;
    _bottomCollectionView.backgroundColor = [UIColor whiteColor];
    
    [_bottomCollectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"FirstCollectionViewCell"];
}


//协议方法实现
-(void)skipVC:(BaseViewController *)VC{
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)skipNewVC:(BaseViewController *)VC{
    
    [self presentViewController:VC animated:YES completion:^{
        
    }];
}



#pragma mark - collectionView协议方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == _collectionView) {
        
        TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TopCollectionViewCell" forIndexPath:indexPath];
        cell.nameStr = [NSString stringWithFormat:@"%@", _topArr[indexPath.row]];
        cell.backgroundColor = [UIColor clearColor];
        
        if (indexPath.row == 0) {
            
            cell.label.textColor = [UIColor orangeColor];
            cell.tag = 1000;
            
        }
        
        return cell;
        
    } else {
        
        if (indexPath.row == 0) {
            
            FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstCollectionViewCell" forIndexPath:indexPath];
            cell.currentNum = indexPath.row;
            cell.firstModelArr = _tableCellModelArr;
            cell.delegate = self;
            return cell;
        }
        
        if (indexPath.row == 1) {
            
            FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstCollectionViewCell" forIndexPath:indexPath];
            cell.delegate = self;
            cell.currentNum = indexPath.row;
            cell.currentNum = indexPath.row;
            cell.secModelArr = _tableCellSecModelArr;
            return cell;

        }
        
        if (indexPath.row == 2) {
            FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstCollectionViewCell" forIndexPath:indexPath];
            cell.currentNum = indexPath.row;
            cell.delegate = self;
            return cell;
            
        }
        
        return nil;
       
    }
   
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    if (collectionView == _collectionView) {
        return 3;
    }
    
    if (collectionView == _bottomCollectionView) {
        return _num;
    }
    
    return 0;
    
   
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _collectionView) {
        TopCollectionViewCell *cell = (TopCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.label.textColor = [UIColor orangeColor];
        cell.backgroundColor = [UIColor clearColor];
        
        [_bottomCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:YES];
        
        _pageCount = indexPath.row;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            _lineView.frame = CGRectMake(10 + indexPath.row * self.view.frame.size.width / 3, 35, (self.view.frame.size.width - 150) / 2, 2);
            
            
        } completion:^(BOOL finished) {
            
        }];
              if (indexPath.row != 0) {
            TopCollectionViewCell *cell = [collectionView viewWithTag:1000];
            cell.label.textColor = [UIColor blackColor];
        }

    }
}


-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _collectionView) {
        
        TopCollectionViewCell *cell = (TopCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.label.textColor = [UIColor blackColor];
        
        }
}





-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _bottomCollectionView) {
        
        NSInteger pageCount = _bottomCollectionView.contentOffset.x / SCREEN_WIDTH;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageCount inSection:0];
        TopCollectionViewCell *cell = (TopCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
        cell.label.textColor = [UIColor orangeColor];
        
        [_collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];   /**< 使导航栏相应item处于被选中状态 */
        [UIView animateWithDuration:0.35 animations:^{
            _lineView.frame = CGRectMake(10 + indexPath.row * self.view.frame.size.width / 3, 35, (self.view.frame.size.width - 150) / 2 , 2);
        }];
        
        
        if (pageCount != _pageCount) {
            NSIndexPath *indexPath2 = [NSIndexPath indexPathForItem:_pageCount inSection:0];
            TopCollectionViewCell *cell2 = (TopCollectionViewCell *) [_collectionView cellForItemAtIndexPath:indexPath2];
            cell2.label.textColor = [UIColor blackColor];
        }
        
        _pageCount = pageCount;
        
        
    }

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _collectionView.backgroundColor = [UIColor clearColor];
    //_collectionView.alpha = 0;
   self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    
    self.navigationController.navigationBar.translucent = YES;
    [_bottomCollectionView reloadData];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
